package _7_集合._2_Map;

import org.junit.jupiter.api.Test;

import java.time.DayOfWeek;
import java.util.*;

/**
 *
 * Map
 * https://www.liaoxuefeng.com/wiki/1252599548343744/1265118019954528
 *
 */
public class MapTest {

    /**
     * 遍历
     */

    @Test
    public void MapTest1(){
        Map<String, Integer> map = new HashMap<>();
        map.put("apple",123);
        map.put("pear",456);
        map.put("banana",789);

        System.out.println("遍历方法一：");
        for (String key:map.keySet()){
            int value = map.get(key);
            System.out.print(value + " ");
        }

        System.out.println("\n\n遍历方法二：");
        for (Map.Entry entry:map.entrySet()){
            System.out.print("key: " + entry.getKey());
            System.out.print("  value:" + entry.getValue() + "\n");
        }

        System.out.println("\n遍历方法三：");
        Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String,Integer> mapEntry = iterator.next();
            System.out.print("key: " + mapEntry.getKey());
            System.out.print("  value: " + mapEntry.getValue() + "\n");
        }
    }


    /**
     * EnumMap
     * https://www.liaoxuefeng.com/wiki/1252599548343744/1300277678309410
     */

    @Test
    public void EnumMapTest1(){
        Map<DayOfWeek,String> map = new EnumMap<>(DayOfWeek.class);
        map.put(DayOfWeek.MONDAY,"星期一");
        map.put(DayOfWeek.TUESDAY,"星期二");
        map.put(DayOfWeek.WEDNESDAY,"星期三");
        map.put(DayOfWeek.THURSDAY,"星期四");
        map.put(DayOfWeek.FRIDAY,"星期五");
        map.put(DayOfWeek.SATURDAY,"星期六");
        map.put(DayOfWeek.SUNDAY,"星期日");

        System.out.println(map);

        Iterator<Map.Entry<DayOfWeek,String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<DayOfWeek,String> mapEntry = iterator.next();
            System.out.print("key: " + mapEntry.getKey());
            System.out.print(" value: " + mapEntry.getValue() + "\n");
        }
    }

    /**
     * TreeMap
     * https://www.liaoxuefeng.com/wiki/1252599548343744/1265117109276544
     */

    @Test
    public void TreeMapTest1(){
        Map<String,Integer> map = new TreeMap<>();
        map.put("pear",1);
        map.put("apple",2);
        map.put("orange",3);

        Iterator<Map.Entry<String,Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String,Integer> mapEntry = iterator.next();
            System.out.println("key: " + mapEntry.getKey() + " value: " + mapEntry.getValue());
        }
    }

}
